'''
fuction : 客户端界面
author  : 辛天宇
date    : 2024-4-12
-------------------------------
author date  modify

'''
import PySimpleGUI as sg
import tool
import global_var

# 调用显示框架
def show_window(theme):
    # 是否使用自定义标题栏
    use_custom_titlebar = False
    # 设置主题
    sg.theme(theme)
    # 创建菜单
    Menu = sg.Menu
    # 左部layout
    layout_l =  [
                    [tool.name('NetWork'), sg.Button('Connect', key='Connect')],
                    [tool.name('NetWork'), sg.Button('Disconnect', key='Disconnect')],
                    [tool.name('NetWork'), sg.Output(size=(32, 1), key='IP')],
                    [sg.Checkbox('Input', use_custom_titlebar, enable_events=True, key='input')],
                    [sg.InputText(disabled=True, key='txbuff'), sg.Button('SEND', key='send')],
                    [sg.Slider(range=(0, 100), orientation='h', size=(29, 20), default_value=0, key='DAC', enable_events=True), sg.Text('DAC', size=(5,2), justification='c',pad=(0,(20,0)), font='Courier 14')],
                    [sg.InputText(key='AT24C02_I', default_text=''), sg.Button('AT24C02 WRITE', key='AT24C02_W')],
                ]
    # 右部layout
    layout_r  = [
                    [sg.Output(size=(16, 1), key='LED_O'), sg.Button('LED', key='LED')],
                    [sg.Output(size=(16, 1), key='SR501_O'), sg.Button('SR501', key='SR501')],
                    [sg.Output(size=(16, 1), key='SR04_O'), sg.Button('SR04', key='SR04')],
                    [sg.Output(size=(16, 1), key='IRDA_O'), sg.Button('IRDA', key='IRDA')],
                    [sg.Output(size=(16, 1), key='DHT11_O'), sg.Button('DHT11', key='dht11')],
                    [sg.Output(size=(16, 1), key='DS18B20_O'), sg.Button('DS18B20', key='ds18b20')],
                    [sg.Output(size=(16, 1), key='AT24C02_O'), sg.Button('AT24C02 READ', key='AT24C02_R')],
                    #[tool.name('Text'), sg.Button('IIC', key='IIC')],
                ]
    # 修饰
    topic = tool.add_stars_to_string(global_var.TOPIC, 5)
    # 设置字体为14号，无特殊样式，通常字体名称和大小用元组表示  
    topic_font = ('_', 24)
    # 整体layout
    layout = [
                [sg.T(topic, text_color='blue', justification='c', font=topic_font)],
                [sg.Col(layout_l), sg.Col(layout_r)],
                [sg.Text('Output:', size=(7,1), justification='r',pad=(0,0), font='Courier 10'),],
                [sg.Output(size=(96, 8), key='Output')],
                [sg.Button('Clean', key='Clean'), sg.Button('Quit', key='Quit')]
            ]

    window = sg.Window('The PySimpleGUI Element List', layout, finalize=True, keep_on_top=True)
    return window

# 处理事件
def event_handle():
    window = show_window('DefaultNoMoreNagging')
    # 事件循环  
    while True:  
        try:
            event, values = window.read()
            if event == 'Exit':  
                break
            if event == 'dht11':
                message = f"{global_var.TEM}°C   {global_var.HUM}%"
                window['Getvalue'].update(message)
            if event == 'ds18b20':
                message = f"{global_var.TEM}°C"
                window['Getvalue'].update(message)
            if event == 'input': 
                print(f"INPUT-----------------") 
            elif event == 'Quit':  
                print(f"See you.............")
                break
            # elif event == 'Connect':
            #     global_var.SERVER_IP = "192.168.5.10"
            #     window['IP'].update(global_var.SERVER_IP)
            # elif event == 'Disconnect':  
            #     global_var.SERVER_IP = "connectionless network service"
            #     window['IP'].update(global_var.SERVER_IP)
            elif event is None:
                print(f"xxxxxxxxxxxxxxxxxxxx")
                break
            elif event == 'LED':  
                print(f"LED-----------------") 
            # 处理其他事件...
        except Exception as e:
            window.close()
            print(f"An error occurred: {e}")
            return 0
    window.close()
    return 0  

def main():
    # theme参考/client/README
    event_handle()

if __name__ == '__main__':
    main()
